﻿/**
 * 默认加载
 */
$(function () {
    addVisit();
});

/**
 * 新增访问量
 */
function addVisit() {
    var referrer_url = document.referrer;
    var visit_path = window.location.href;
    // console.log(`visit_path=${visit_path}`)
    var client_cookies = document.cookie;
    var device_language = navigator.language;
    var search_engine = 'PC';
    var client_browser = '';
    var source_id = 1;
    var device_version = '';
    var device_system = '';
    var device_model = '';
    var device_platform = '';
    var device_brand = '';
    var device_info = '';
    var client_id = getCookie("CLOUDIN_MINIX");
    // console.log(`client_id=${client_id}`)
    if (client_id != "") {
        client_id = getCookie("CLOUDIN_MINIX");
    } else {
        client_id = uuid();
        setCookie("CLOUDIN_MINIX", client_id, 30)
    }
    // console.log(`isMobile=${isMobile()}`)

    if (isMobile()) {
        search_engine = 'H5';
        source_id = 4;
        // 判断数组中是否包含某字符串
        Array.prototype.contains = function (needle) {
            for (i in this) {
                if (this[i].indexOf(needle) > 0)
                    return i;
            }
            return -1;
        }
        var device_type = navigator.userAgent;
        var md = new MobileDetect(device_type);
        device_platform = md.mobile();
        device_brand = md.phone();
        device_info = md.userAgent();
        var os = md.os();
        var model = "";
        if (os == "iOS") {
            os = md.os() + md.version("iPhone");
            model = md.mobile();
        } else if (os == "AndroidOS") {
            os = md.os() + md.version("Android");
            var sss = device_type.split(";");
            var i = sss.contains("Build/");
            if (i > -1) {
                model = sss[i].substring(0, sss[i].indexOf("Build/"));
            }
        }
        device_system = os;
        device_model = model;
        device_version = md.version('Webkit');
        // console.log(`device_type=${device_type},os=${os}model=${model}`);
    } else {
        search_engine = 'PC';
        source_id = 1;
        client_browser = getExplore();
        device_info = navigator.userAgent;
        var appName = navigator.appName;
        var appCodeName = navigator.appCodeName;
        var appVersion = navigator.appVersion;
        var appMinorVersion = navigator.appMinorVersion;
        device_platform = navigator.platform;
        device_language = navigator.language;
        // console.log(`device_info=${device_info},appName=${appName},appCodeName=${appCodeName},appVersion=${appVersion},device_language=${device_language}`);
    }

    var member_id = localStorage.getItem("CLOUDIN_MINIX_MEMBER_ID");
    if (!member_id) {
        member_id = -1;
    }

    // 提交至服务端
    $.ajax({
        headers: {
            'RequestVerificationToken': csrfToken
        },
        type: "POST",
        url: "/visits/create",
        data: {
            "referrer_url": referrer_url,
            "search_engine": search_engine,
            "client_browser": client_browser,
            "visit_path": visit_path,
            "client_info": "",
            "client_cookies": client_cookies,
            "device_system": device_system,
            "device_platform": device_platform,
            "device_brand": device_brand,
            "device_model": device_model,
            "device_language": device_language,
            "device_version": device_version,
            "device_info": device_info,
            "temp_info": "",
            "member_id": member_id,
            "company_id": -1,
            "client_id": client_id,
            "source_id": source_id,
            "channel_id": 2,
            "client_version": "1.0.6",
            "visit_remark": ""
        },
        dataType: "json",
        contentType: "application/json",
        cache: false,
        success: function (res) {
            console.log(`res=${JSON.stringify(res)}`);
        },
        error: function (e) {
            // console.log(e.status);
        }
    });
}
/**
 * 判断是否移动端
 */
function isMobile() {
    var regex_match = /(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|jigs browser|hiptop|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220)/i;
    var u = navigator.userAgent;
    if (null == u) {
        return true;
    }
    var result = regex_match.exec(u);
    if (null == result) {
        return false;
    } else {
        return true;
    }
}
/**
 * 获取浏览器名称
 */
function getExplore() {
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
        (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] :
            (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] :
                (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] :
                    (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] :
                        (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] :
                            (s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0;
    // 根据关系进行判断
    if (Sys.ie) return ('IE: ' + Sys.ie);
    if (Sys.edge) return ('EDGE: ' + Sys.edge);
    if (Sys.firefox) return ('Firefox: ' + Sys.firefox);
    if (Sys.chrome) return ('Chrome: ' + Sys.chrome);
    if (Sys.opera) return ('Opera: ' + Sys.opera);
    if (Sys.safari) return ('Safari: ' + Sys.safari);
    return 'Unkonwn';
}
/**
 * 生成UUID
 */
function uuid() {
    var s = [];
    var hexDigits = "0123456789abcdef";
    for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[14] = "4";
    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1); // bits 6-7 of the clock_seq_hi_and_reserved to 01
    s[8] = s[13] = s[18] = s[23] = "-";

    var uuid = s.join("");
    return uuid;
}
/**
 * 设置Cookie
 */
function setCookie(cname, cvalue, exdays) {
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires=" + d.toGMTString();
    document.cookie = cname + "=" + cvalue + "; " + expires;
}
/**
 * 获取Cookie
 */
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name) == 0) { return c.substring(name.length, c.length); }
    }
    return "";
}